/*
 *  $Id: Enchantments.cs 1083 2010-05-24 10:28:38Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    /// <summary>
    /// Содержимое таба, определяющее имеющиеся на объекте эффекты
    /// </summary>
    public class EnchantmentsTabContent: TabContent {
        private AbstractObject _owner;
        private List<Region> _regions;

        public EnchantmentsTabContent(AbstractObject owner): base(TabType.Enchantments, false) {
            _owner = owner;
            _regions = new List<Region>();
            for (int i = 0; i < Constants.FieldSize; i++)
                _regions.Add(new EnchantmentRegion(Constants.GetRowRect(i, 3), this, i));
        }

        public static EnchantmentsTabContent GetContent(Tabs tabs, AbstractObject owner) {
            EnchantmentsTabContent content = null;
            if (tabs != null) content = tabs.FindContent<EnchantmentsTabContent>(delegate(EnchantmentsTabContent other) {
                return other.Owner == owner;
            });
            return content != null ? content : new EnchantmentsTabContent(owner);
        }

        public AbstractObject Owner { get { return _owner; } }

        public List<Affect> Enchantments { get { return _owner.Summary.GetEnchantments(); } }

        public override int Count {
            get { return Enchantments.Count; }
        }

        public override IEnumerable<Region> GetRegions() {
            for (int i = 0; i < CountFromPosition; i++)
                yield return _regions[i];
        }

        public Affect GetEnchantment(int num) {
            int index = GetIndex(num);
            return (index >= 0 && index < Count) ? Enchantments[index] : null;
        }
    }

    /// <summary>
    /// Регион, определяющий эффект
    /// </summary>
    public class EnchantmentRegion: HelpRegion {
        private EnchantmentsTabContent _content;
        private int _num;
        private Affect _enchantment;
        private AffectIconDictionary _icons;
        private AffectIconItems _items;
        private AffectIconHint _hint;

        public EnchantmentRegion(Rectangle rect, EnchantmentsTabContent content, int num): base(rect) {
            _content = content;
            _num = num;
            _enchantment = null;
            _icons = new AffectIconDictionary();
            _items = new AffectIconItems(6);
            _hint = new AffectIconHint();
        }

        public Affect Enchantment { get { return _content.GetEnchantment(_num); } }

        public override Hint GetHint() {
            _items.Update(_icons, Rect.Location);
            return _hint.GetHint(_items.FindByMouse());
        }

        public override void Draw() {
            Affect enchantment = Enchantment;
            if (enchantment == null) return;
            if (_enchantment != enchantment) {
                _enchantment = enchantment;
                _icons.Clear();
                foreach (Affect affect in enchantment.GetIconAffects())
                    _icons.Add(affect);
            }
            enchantment.SmallPicture.Draw(Rect.Location);
            _items.Update(_icons, Rect.Location);
            _items.Draw();
        }
        public override void DrawTarget() {
            Draw();
            Data.CardSelect1.Draw(Rect.Location);
        }

        public override IHelpProvider HelpObject {
            get { return Enchantment; }
        }
    }
}